package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "菜品管理接口")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result insert(@RequestBody DishDTO dishDTO){
        dishService.insert(dishDTO);

        //清理缓存
        redisTemplate.delete("key_"+dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult=dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){ //-->@RequestParam在这里的作用是将字符串（1,2,3）自动转化为List<Long>类型
        dishService.delete(ids);

        //将缓存全删了
        Set keys = redisTemplate.keys("key_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    @ApiOperation("修改菜品时信息回显")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id)
    {
        DishVO dishVO=dishService.getById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);

        //将缓存全删了
        Set keys = redisTemplate.keys("key_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(Long categoryId){
        List<Dish> list=dishService.selectByCategoryId(categoryId);
        return Result.success(list);
    }

//    @PostMapping("/status/{status}")
//    public Result startorstop(@PathVariable Integer status,Long id){
//
//    }
}
